package casa.depesos.app.com.frag;

import android.widget.TextView;

import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.GetCodeBean;
import casa.depesos.app.com.http.data.CodeType;
import casa.depesos.app.com.http.req.GetCodeReq;
import casa.depesos.app.com.utils.Report;

public abstract class SmsBaseFragment extends BaseFragment {
    boolean setSms = false;

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected abstract String getPhone();

    protected abstract CodeType getCodeType();

    protected void getCode() {
        showLoading();
        Report.click("ACTION_NN_004");
        new GetCodeReq(getPhone(), getCodeType()).req(new ReqCallback<GetCodeBean>() {
            @Override
            public void callback(boolean success, GetCodeBean bean) {
                hideLoading();
                if (success && bean.isValid()) {
                    countDown();
                }
            }
        });
    }

    int left = 60;

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (--left <= 0) {
                timeOut();
            } else {
                getCodeTv().setText(left + "s");
                h.postDelayed(this, 1000);
            }
        }
    };

    protected void timeOut() {
        getCodeTv().setText("retry");
        getCodeTv().setEnabled(true);
    }

    protected abstract TextView getCodeTv();

    protected void countDown() {
        getCodeTv().setEnabled(false);
        getCodeTv().setText("60s");
        left = 60;
        h.removeCallbacks(runnable);
        h.postDelayed(runnable, 1000);
    }
}
